// b2c/center/center.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{
      avatarUrl:"../../images/public/Head.png"
    },
    hasUserInfo:false,
    windowH:0,
    cookie:"",
    rotun:null,
    myp:null,
    stat:[
      {
        src:"../../images/icon/d.png",
        text:"待付款",
        itype:"已提交"
      },
      {
        src: "../../images/icon/dfh.png",
        text: "待发货",
        itype: "已受理"
      },
      {
        src:"../../images/icon/ywc.png",
        text:"已发货",
        itype:"已发货"
      },
      {
        src:"../../images/icon/dpj.png",
        text:"待评价",
        itype:"已完成"
      }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    if (app.globalData.userInfo){
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo:true
      })
      console.log(this.data.userInfo)
    }else{
      app.userInfoReadyCallback = res =>{
        this.setData({
          userInfo:res.userInfo,
          hasUserInfo:true,
          
        })
      }
    }
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    var self = this;
    var a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a
    })    
    wx.request({
      url: 'https://csb2c.fenxiangqu.cn/cxzb_000016x',
      data: {
        x: ''
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + this.data.cookie
      },
      success: function (res) {
        console.log(res)
        if(res.data !== "login"){
          var user = res.data[0].username,
              str = user.slice(3,8),
              newUser = user.replace(str,"*****");
          self.setData({
            rotun:true,
            myp: newUser
          })
        }else{
          self.setData({
            rotun:false,
            myp: "登录/注册后方可获取更多权限哟！"
          })
        }
      },
      fail:function(res){
        console.log(res)
      }
    })
    wx.hideLoading()
  },
  loginNo:function(){
    var self = this;
    wx.showLoading({
      title: '退出中',
    })
    
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_10011_wx",
      data: {
        a:"aaaaa"
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + this.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        wx.hideLoading();
        if(res.data.ret == "1"){
          try {
            wx.setStorageSync('cookie', '');
            wx.showToast({
              title: '退出成功',
              icon: 'success',
              duration: 1000
            })
            self.setData({
              rotun: false,
              myp: "登录/注册后方可获取更多权限哟！"
            })

          } catch (e) {
            console.log
          }
        }
        
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res)
      }
    })
  },
  orderNow:function(){
    wx.navigateTo({
      url: "../order/order"
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    console.log(e)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})